﻿using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Security.Principal;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using System.Net.Mail;
using System.Net;
using System.Security.Cryptography;
using System.Net.Security;
using System.Data;
using System.Configuration;


using ElCharro01.Helpers;
using ElCharro01.Models;
using System.Text;
namespace ElCharro01.Manager
{
    public class UserManager
    {
        Entities db = new Entities();
        private IMembershipService MembershipService = new AccountMembershipService();
        public bool SendResetPasswordMail(int id)
        { 
            String password = GetPassword();
            try
            {   
                User user = db.UserSet.Single(u => u.ID == id);
                //aggiorno la password nel Model
                bool result = MembershipService.ChangePassword(user.Username, user.Password, password);
                
                //aggiorno la password nel db
                user.Password = password;
                db.ObjectStateManager.ChangeObjectState(user, EntityState.Unchanged);
                db.UserSet.Attach(user);
                db.ObjectStateManager.ChangeObjectState(user, EntityState.Modified);
                db.SaveChanges();

                String message = "<h3>Reimposta Password " + "</h3><br />"+
                                 "Il tuo username è: " + user.Username + "<br />" +
                                 "La tua password provvisoria è: " + password + "<br />" + "<br />" +
                                 "Puoi Cambiare la tua password attraverso il pannello cambia Password.";

                /* Send Mail */
                MailHelper.SendMailMessage(user.Email,
                                           "",
                                           "",
                                           "Reimposta Password " + System.Configuration.ConfigurationManager.AppSettings["Title"],
                                           message,
                                          ConfigurationManager.AppSettings["Email"]);
               
            }
            catch (Exception ex)
            {
                return false;
            }

            return true;
        }




        #region Funzioni Per La Generazione Password Casuale
        private string RandomString(int size, bool lowerCase)
        {
           StringBuilder builder = new StringBuilder();
            Random random = new Random();
            char ch;
            for (int i = 0; i < size; i++)
            {
                ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
                builder.Append(ch);
            }
            if (lowerCase)
                return builder.ToString().ToLower();
            return builder.ToString();
        }

        private int RandomNumber(int min, int max)
        {
            Random random = new Random();
            return random.Next(min, max);
        }

        public string GetPassword()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(RandomString(4, true));
            builder.Append(RandomNumber(1000, 9999));
            builder.Append(RandomString(2, false));
            return builder.ToString();
        }
        #endregion

    }
}